using System;
using System.IO;
using System.Windows.Forms;
using HestiaBackup.Library.ConfigurationManager;

namespace HestiaBackup.Gui.Forms
{
    public partial class MainForm : Form
    {
        private string fileName;
        private Settings settings;

        public MainForm()
        {
            InitializeComponent();
            ftpProjectsList.AutoGenerateColumns = false;
            ftpProjectsList.RowsAdded += new DataGridViewRowsAddedEventHandler(ftpProjectsList_RowsAdded);
        }

        void ftpProjectsList_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            if (settings.FtpSettings[e.RowIndex].LastRunSuccess)
            {
                ftpProjectsList.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Green;
            }
            else
            {
                ftpProjectsList.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Red;                
            }
        }

        private void openProjectFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.CheckFileExists = true;
            openFile.InitialDirectory = Directory.GetCurrentDirectory();
            openFile.Multiselect = false;
            openFile.Title = "Please select the configuration file";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                fileName = openFile.FileName;
                try
                {
                    settings = new Settings(fileName);
                    LoadProjectsData();
                }
                catch
                {
                    MessageBox.Show("Error, specified file isn't in correct format", "Error during file open", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void LoadProjectsData()
        {
            ftpProjectsList.DataSource = settings.FtpSettings;
            saveProjectFileAsToolStripMenuItem.Enabled = true;
            saveProjectFileToolStripMenuItem.Enabled = true;
            btnNewProject.Enabled = true;
            btnEditFtp.Enabled = true;
        }

        private void newProjectFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(fileName))
            {
                if (MessageBox.Show("A project file is already open, do you want to save changes ?", "Save changes ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    settings.SaveData();
                }
            }
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.AddExtension = true;
            saveFile.DefaultExt = ".xml";
            saveFile.InitialDirectory = Directory.GetCurrentDirectory();
            saveFile.OverwritePrompt = true;
            saveFile.Title = "Choose a file name";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                fileName = saveFile.FileName;
                settings = new Settings(fileName);
                settings.SaveData();
                settings.FtpSettings.Clear();
                LoadProjectsData();
            }
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(fileName))
            {
                if (MessageBox.Show("A project file is already open, do you want to save changes ?", "Save changes ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    settings.SaveData();
                }
            }
            Close();
        }

        private void saveProjectFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(fileName))
            {
                settings.SaveData();
                MessageBox.Show("Project file successfully saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("No project file to save", "No project", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void saveProjectFileAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.AddExtension = true;
            saveFile.DefaultExt = ".xml";
            saveFile.InitialDirectory = Directory.GetCurrentDirectory();
            saveFile.OverwritePrompt = true;
            saveFile.Title = "Choose a file name";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                fileName = saveFile.FileName;
                settings.FileName = fileName;
                settings.SaveData();
                MessageBox.Show("Project file successfully saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnNewProject_Click(object sender, EventArgs e)
        {
            EditFtpProject eftpProject = new EditFtpProject(new FtpSetting());
            eftpProject.ShowDialog();
            if (eftpProject.IsDataCorrect)
            {
                settings.FtpSettings.Add(eftpProject.Setting);
                settings.SaveData();
                settings = new Settings(fileName);
                LoadProjectsData();
            }
        }

        private void btnEditFtp_Click(object sender, EventArgs e)
        {
            FtpSetting setting = settings.FtpSettings[ftpProjectsList.CurrentRow.Index];
            EditFtpProject eftpProject = new EditFtpProject(setting);
            eftpProject.ShowDialog();
            if (eftpProject.IsDataCorrect)
            {
                settings.FtpSettings[ftpProjectsList.CurrentRow.Index] = eftpProject.Setting;
                LoadProjectsData();
            }
        }
    }
}